package org.nlp2rdf.navigator.component.store;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dllearner.utilities.Files;
import org.nlp2rdf.navigator.client.common.LearnedConceptDTO;
import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;
import org.nlp2rdf.navigator.component.exception.ConceptSavingFailedException;

/**
 * 
 * Stores the objects as serialized java object to a file, as given in filename
 * 
 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
 * 
 */
public class SimpleFileStore extends Store implements IStore {
	private static final Logger logger = Logger.getLogger(SimpleFileStore.class);
	private Map<String, StoredConceptDTO> conceptMap = null;
	private String filename;

	public void setFilename(String filename) {
		this.filename = filename;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nlp2rdf.navigator.server.store.Store#_init()
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void _init() throws ComponentInitException {
		this.filename = getComponentConfig().getPathModifier() + this.filename;
		File f = new File(filename);
		conceptMap = new HashMap<String, StoredConceptDTO>();
		if (f.exists()) {
			conceptMap = (Map<String, StoredConceptDTO>) Files.readObjectfromFile(f);
            updateRelatedConceptStrategies(conceptMap);
			logger.debug("Loaded " + conceptMap.size() + " concepts from " + filename);
		} else {
			logger.warn("No saved concepts found in " + filename);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nlp2rdf.navigator.server.store.IStore#loadConcepts()
	 */
	public List<StoredConceptDTO> getStoredConcepts() {
		check();
		return new ArrayList<StoredConceptDTO>(conceptMap.values());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.nlp2rdf.navigator.server.store.Store#_saveConcept(org.nlp2rdf.navigator
	 * .client.common.LearningResultDTO)
	 */
	@Override
	protected StoredConceptDTO _saveConcept(LearnedConceptDTO learningResultDTO)
			throws ConceptSavingFailedException {
		check();
		StoredConceptDTO s = new StoredConceptDTO(learningResultDTO, generateUriFromLabel(learningResultDTO));
		put(s);
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.nlp2rdf.navigator.server.store.IStore#incrementHitCount(org.nlp2rdf
	 * .navigator.client.common.LearningResultDTO)
	 */
	public void incrementHitCount(StoredConceptDTO learningResultDTO) {
		check();
		if (conceptMap.get(learningResultDTO.getManchesterSyntaxString()) != null) {
			conceptMap.get(learningResultDTO.getManchesterSyntaxString()).incrementHitcounter();
		}
		_saveConcept(learningResultDTO);
	}

	private void put(StoredConceptDTO learningResultDTO) {
		String key = learningResultDTO.getManchesterSyntaxString();
		conceptMap.put(key, learningResultDTO);
		File f = new File(filename);
		Files.writeObjectToFile(conceptMap, f);
		logger.debug("Saved " + conceptMap.size() + " concepts to " + filename);
	}

}
